package cn.etgps.etgps.ui;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import cn.etgps.etgps.R;
import cn.etgps.etgps.base.BaseActivity;
import cn.etgps.etgps.blmanager.UserManager;
import cn.etgps.etgps.dialog.DialogCreater;
import cn.etgps.etgps.utils.Constant;
import cn.etgps.etgps.view.TitleBar;

/**
 * 用来用户提交反馈
 */
public class FeedBackActivity extends BaseActivity implements View.OnClickListener {
    private TitleBar mTitlebarView = null;
    private EditText mFeedBack = null;
    /**
     * 用户写的评论内容
     */
    private String mComment = "";
    private Button mCommit = null;
    private UserManager mUserManger = null;
    /**
     * 向服务器发送反馈信息的线程
     */
    private SendFeedbackTask mSendFeedbackTask = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initTitle();
        initView();
        setUpBtn();
    }

    /**
     * 标题
     */
    private void initTitle() {
        mTitlebarView = (TitleBar) findViewById(R.id.title_bar);
        mTitlebarView.initTitleBarInfo(getString(R.string.label_feedback_title), "", R.drawable.icon_return, "", 0,
                false);
        mTitlebarView.setMainClickListener(this, getString(R.string.btn_cancel), "");
    }

    /**
     * 初始化控件
     */
    private void initView() {
        mUserManger = new UserManager(this);
        mFeedBack = (EditText) findViewById(R.id.feedBack);
        mCommit = (Button) findViewById(R.id.commit);
    }

    /**
     * 提交按钮监听
     */
    private void setUpBtn() {
        mCommit.setOnClickListener(this);
    }

    @Override
    public void clickButtonListener(String name) {
        if (name.equals(getString(R.string.btn_cancel))) {
            this.finish();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.commit:
                mComment = mFeedBack.getText().toString().trim();
                if (isRightInput(mComment)) {
                    //首先进行网络判断
                    if (mUserManger.checkNetwork(this)) {
                        if (mSendFeedbackTask != null && mSendFeedbackTask.getStatus() == AsyncTask.Status.RUNNING) {
                            mSendFeedbackTask.cancel(true);
                            mUserManger.closeDBManager();
                        }
                        //再发送反馈
                        mSendFeedbackTask = new SendFeedbackTask();
                        mSendFeedbackTask.execute(mComment);
                    } else {
                        mUserManger.showToastMessage(getString(R.string.toast_no_network), 1000);
                    }
                } else {
                    mUserManger.showToastMessage(getString(R.string.label_feedback_invalid_input), 1000);
                }

                break;
        }
    }

    /**
     * 判断用户输入是否为空
     *
     * @param mComment 用户的输入信息
     * @return true为有输入，false为无输入
     */
    private boolean isRightInput(String mComment) {
        if (mComment == "" || mComment.isEmpty() || mComment == null || mComment.length() == 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 向服务器发送反馈信息
     */
    public class SendFeedbackTask extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {
            showNormalDialog(DialogCreater.DIALOG_PROGRESS);
            return mUserManger.saveFeedback(mComment);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            dismissNormalDialog();
            if (result.equals(Constant.RETURN_RESULT_OK)) {
                mUserManger.showToastMessage(getString(R.string.label_feedback_ok_toast), 2000);
                finish();
            } else if (Constant.CONNECT_TO_SERVER_FAILED.equals(result)) {
                mUserManger.showToastMessage(getString(R.string.toast_connect_server_failed), 2000);
            } else {
                mUserManger.showToastMessage(result, 2000);
            }
        }
    }
}
